package com.gagakuai.engine.impl.pvm.runtime;

import com.gagakuai.engine.impl.pvm.PvmEvent;
import com.gagakuai.engine.impl.pvm.process.ActivityImpl;
import com.gagakuai.engine.impl.pvm.process.ProcessDefinitionImpl;
import com.gagakuai.engine.impl.pvm.process.ScopeImpl;
import com.gagakuai.engine.impl.runtime.InterpretableExecution;

import java.util.List;

/*
 * @program: common-starters
 * @description: 流程开始处理原子执行类
 * @author: houhong
 * @create: 2023-01-02 19:24
 */
public class AtomicOperationProcessStart extends AbstractEventAtomicOperation {

    @Override
    protected String getEventName() {
        //开始事件
        return PvmEvent.EVENTNAME_START;
    }

    @Override
    protected ScopeImpl getScope(InterpretableExecution execution) {

        return execution.getProcessDefinition();
    }

    /*
     * 获取到开始节点处理完毕，做一些处理完毕后的逻辑，然后执行下一个原子类
     */
    @Override
    protected void eventNotificationsCompleted(InterpretableExecution execution) {

        //// TODO: 2023/1/2  使用事件发布器 发布事件


        //获取流程定义
        ProcessDefinitionImpl processDefinition = execution.getProcessDefinition();
        //获取开始执行实列 对象， 该对象拥有 开始节点在pvm中的表示类ActivityImpl
        StartingExecution startingExecution = execution.getStartingExecution();
        //确保开始节点已经存储在 processDefinition中
        List<ActivityImpl> initialActivityStack = processDefinition.getInitialActivityStack(startingExecution.getInitial());
        //将开始节点存储在execution中，方便获取到下一个节点
        execution.setActivity(initialActivityStack.get(0));
        //触发下一个原子类型处理 ---流程启动准备原子类
        execution.performOperation(PROCESS_START_INITIAL);
    }


}